package cn.webrx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class T2 {
    public static void main(String[] args) {
        String pwd = PasswordField.readPassword("请输入密码: ");
        System.out.println(pwd);
    }
}

class EraserThread implements Runnable {
    private boolean stop;

    public EraserThread(String prompt) {
        System.out.print(prompt);
    }

    public void run() {
        stop = true;
        while (stop) {
            System.out.print("\010*");
            try {
                Thread.currentThread().sleep(1);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }
    public void stopMasking() {
        this.stop = false;
    }
}


class PasswordField {
    public static String readPassword(String prompt) {
        EraserThread et = new EraserThread(prompt);
        Thread mask = new Thread(et);
        mask.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String password = "";
        try {
            password = in.readLine();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        et.stopMasking();
        return password;
    }
}
